var fs = require('fs');
var hljs = require('highlight.js');
var md = require('markdown-it')({
  html:         false,        // Enable HTML tags in source
  xhtmlOut:     false,        // Use '/' to close single tags (<br />).
                              // This is only for full CommonMark compatibility.
  breaks:       false,        // Convert '\n' in paragraphs into <br>
  langPrefix:   'language-',  // CSS language prefix for fenced blocks. Can be
                              // useful for external highlighters.
  linkify:      false,        // Autoconvert URL-like text to links
 
  // Enable some language-neutral replacement + quotes beautification
  typographer:  false,
 
  // Double + single quotes replacement pairs, when typographer enabled,
  // and smartquotes on. Could be either a String or an Array.
  //
  // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  quotes: '“”‘’',
 
  // Highlighter function. Should return escaped HTML,
  // or '' if the source string is not changed and should be escaped externally.
  // If result starts with <pre... internal wrapper is skipped.
  highlight: function (str, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(lang, str).value;
      } catch (__) {
        console.error(__);
      }
    }
    return ''; // use external default escaping
  }
});
/**
 * 配置对应的文件后缀以什么语言渲染 markdown
 */
const SUFFIXS = {
  json:'json',
  babelrc:'json',
  h:'c++',
  cpp:'c++',
  cc:'c++',
  vue:'html'
}

// 获取文件内容
exports.getFileText = function(openPath,suffix){
  // 文件内容
  let fileText = fs.readFileSync(openPath, 'utf8');
  return {
    text:mdRender(fileText,suffix)
  }
}
/**
 * 
 * @param {string}} text 要渲染的内容
 * @param {string} suffix 文件后缀
 * @returns {string}
 */
exports.mdRender = function(text,suffix){
  let renderSuffix = SUFFIXS[suffix] || suffix;
  console.log(`
    获取的后缀：${suffix}
    渲染的后缀: ${renderSuffix}
  `)
  return md.render('```' + renderSuffix + '\n'+ text)
}
